package web;


import spring.cloud.tasks.client.Client;
import spring.cloud.tasks.client.item_task_processor.Task;
import spring.cloud.tasks.server.util.TaskItemDatas;

import javax.servlet.http.HttpServletRequest;

public class taskTypeDeal {
    public static void main(String[] args) {
        HttpServletRequest request = null;
        String action = request.getParameter("action");
        String result = "";
        boolean isRefreshParent = false;
        String baseTaskType = request.getParameter("task");
        try {
            if (action.equalsIgnoreCase("createTaskType") || action.equalsIgnoreCase("editTaskType")) {
                isRefreshParent = false;
                Task task = new Task();
                task.setTaskId(baseTaskType);

                task.setFetchNum(request.getParameter("fetchNum") == null ? 0 : Integer.parseInt(request.getParameter("fetchNum")));
                task.setMaxExecuteNum(request.getParameter("maxExecuteNum") == null ? 0 : Integer.parseInt(request.getParameter("maxExecuteNum")));
                task.setSleepTimeWhenNoData(request.getParameter("sleepTimeWhenNoData") == null ? 0 : (int) (Double.parseDouble(request.getParameter("sleepTimeWhenNoData")) * 1000));

                task.setProcessorType(request.getParameter("processType"));
                //task.setExpireOwnSignInterval(request.getParameter("expireOwnSignInterval")==null?0: Integer.parseInt(request.getParameter("threadCountPerTaskProcessor")));
                task.setCronExpressionString(request.getParameter("planRunStartTime"));
                String s = request.getParameter("maxTaskItemsOfOneThreadGroup");

                task.setTaskParameter(request.getParameter("taskParameter"));

                String itemDefines = request.getParameter("taskItemData");
                itemDefines = itemDefines.replaceAll("\r", "");
                itemDefines = itemDefines.replaceAll("\n", "");
                task.setTaskItems(TaskItemDatas.splitTaskItem(itemDefines));
                task.setStatus(request.getParameter("$taskItemStatus"));
                if (action.equalsIgnoreCase("createTaskType")) {
                    Client.getZooKeeperDataManager().createTask(task);
                    result = "����" + baseTaskType + "�����ɹ���������";
                } else {
                    Client.getZooKeeperDataManager().updateTask(task);
                    result = "����" + baseTaskType + "�޸ĳɹ���������";
                }
                isRefreshParent = true;

            } else if (action.equalsIgnoreCase("cleanTask")) {
                Client.getZooKeeperDataManager().cleanTask(
                        baseTaskType);
                result = "����" + baseTaskType + "��������Ϣ����ɹ���������";
                isRefreshParent = false;
            } else if (action.equalsIgnoreCase("deleteTask")) {
                Client.getZooKeeperDataManager().cleanTask(
                        baseTaskType);
                result = "����" + baseTaskType + "ɾ���ɹ���������";
                isRefreshParent = true;
            } else if (action.equalsIgnoreCase("pauseTask")) {
                Client.getZooKeeperDataManager().pauseTask(baseTaskType);
                isRefreshParent = true;
            } else if (action.equalsIgnoreCase("runOrRecoverTask")) {
                Client.getZooKeeperDataManager().runOrRecoverTask(baseTaskType);
                isRefreshParent = true;
            } else {
                throw new Exception("��֧�ֵĲ�����" + action);
            }
        } catch (Throwable e) {
            e.printStackTrace();
            result = "ERROR:" + e.getMessage();
            isRefreshParent = false;
        }
    }
}